package at.guigu.web;

import at.guigu.service.BrandService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BrandServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 获取最大域ServletContext对象
        ServletContext servletContext = request.getServletContext();
        //等同于ServletContext servletContext = request.getSession().getServletContext();

        // 2 获取应用上下文对象（即IOC容器）
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        //3 获取bean
        BrandService brandService = app.getBean(BrandService.class);

        //4 执行要增强的原始方法（即目标方法）
        brandService.update();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}